package com.gitee.zhengjiasen.dp.proxy.jdk;


import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

/**
 * 利用jdk生成一个代理对象.
 */
public class ProxyFactory {


    /**
     * 得到代理对象
     *
     * @return
     */
    public static Object getProxyInstance(IOrderDao orderDao) {
        System.getProperties().put("sun.misc.ProxyGenerator.saveGeneratedFiles", "true");
        return Proxy.newProxyInstance(orderDao.getClass().getClassLoader(),
                orderDao.getClass().getInterfaces(), new InvocationHandler() {
                    @Override
                    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                        System.out.println("检查是否有插入权限");
                        Object invoke = method.invoke(orderDao, args);
                        System.out.println("代理类执行结束");
                        return invoke;
                    }
                });
    }


}
